<?php
# !!!!!!!!!! DON'T MAKE ANY CHANGE IF YOU DON'T KNOW WHAT YOU'RE DOING !!!!!!!!!!
// --------------------------------------------------------------------------------------------------------------
// File Name              :	functions.php
// Last Modification Time	:	2008-02-01 24:00
// Last Modified by       :	turker (turker.biz@gmail.com)
// list of modifications  :
//
//	- 2008-02-01 turker
//		* 24:00 file created.
//
// --------------------------------------------------------------------------------------------------------------
if (eregi("functions.php",$_SERVER['PHP_SELF'])) {
	header ("Location:../index.php");
	exit;
}

function isLogged() {
  if (@$_SESSION['logged']) return true;
  else return false;
}

function isAdmin() {
  /*
	0 : yeni kayıt
	1 : email doğrulamış
	2 : yönetici onaylamış
	3 : rezerve
	4 : rezerve
	5 : yönetici
	*/
  if (@$_SESSION['user']['level']==5) return true;
  else return false;
}

function getActiveUser($key='id') {
  switch ($key) {
    case 'id': return $_SESSION['user']['id']; break;
    case 'email': return $_SESSION['user']['email']; break;
    case 'password': return $_SESSION['user']['password']; break;
    case 'name': return $_SESSION['user']['name']; break;
    case 'lang': return $_SESSION['user']['lang']; break;
    case 'date': return $_SESSION['user']['date']; break;
    case 'level': return $_SESSION['user']['level']; break;
    case 'bdate': return $_SESSION['user']['bdate']; break;
    case 'gender': return $_SESSION['user']['gender']; break;
    case 'phone': return $_SESSION['user']['phone']; break;
    case 'address': return $_SESSION['user']['address']; break;
    case 'city': return $_SESSION['user']['city']; break;
    default: return $_SESSION['user']['id']; break;
  }
}

function loadSettings() {
  global $mysql,$prep,$OPTIONS_TABLE;
  $q=$mysql->query("select * from $OPTIONS_TABLE");
  while ($read=$mysql->fetch($q)) {
    $option=$read['option'];
    $value=$prep->html($read['value']);
    $eval="global \$$option;\$$option='$value';";
    eval($eval);
  }
}

function setMeta($page) {
  global $mysql,$prep,$SITE_TITLE,$SITE_KEYS,$BRANDS_TABLE,
         $PAGES_TABLE,$ITEMS_TABLE,$CATEGORIES_TABLE;

  switch ($page) {
  	case 'activate':
  	  $SITE_TITLE='Üyelik Aktivasyonu | '.$SITE_TITLE;
  	break;
  	case 'add':
  	  $SITE_TITLE='Yönetici İşlemleri | '.$SITE_TITLE;
  	break;
  	case 'basket':
  	  $SITE_TITLE='Sipariş Sepetim | '.$SITE_TITLE;
  	break;
  	case 'brands':
      $title=$prep->html(getTitle($BRANDS_TABLE));
      $SITE_TITLE="$title | $SITE_TITLE";
      $SITE_KEYS.=",$title,marka,ürün";
  	break;
  	case 'category':
  	  $title=$prep->html(getTitle($CATEGORIES_TABLE));
  	  $SITE_TITLE="$title | $SITE_TITLE";
  	  $SITE_KEYS.=",$title,kategori,ürün";
	  break;
    case 'item':
      $title=$prep->html(getTitle($ITEMS_TABLE));
      $SITE_TITLE="$title | $SITE_TITLE";
      $SITE_KEYS.=",$title,ürün";
    break;
  	case 'login':
  	  $SITE_TITLE='Üye Girişi | '.$SITE_TITLE;
  	  $SITE_KEYS.=',giriş,üye';
  	break;
  	case 'lostpass':
  	  $SITE_TITLE='Şifre Hatırlatma | '.$SITE_TITLE;
  	  $SITE_KEYS.=',şifre,hatırla,kayıp,unutulmuş,üye';
  	break;
  	case 'mybasket':
  	  $SITE_TITLE='Sipariş Sepetim | '.$SITE_TITLE;
  	break;
  	case 'myinfo':
  	  $SITE_TITLE='Bana Özel | '.$SITE_TITLE;
  	break;
  	case 'page':
  	  $title=$prep->html(getTitle($PAGES_TABLE));
      $SITE_TITLE="$title | $SITE_TITLE";
      $SITE_KEYS.=",$title,sayfa";
	  break;
    case 'register':
  	  $SITE_TITLE='Yeni Üyelik Başvurusu | '.$SITE_TITLE;
  	  $SITE_KEYS.=',kayıt,başvuru,yeni,üye,kullanıcı,müşteri';
    break;
  	case 'search':
  	  $search=@$prep->html($_GET['search']);
  	  $SITE_TITLE='Arama | Aranan: '.$search.' | '.$SITE_TITLE;
  	  $search=str_replace(' ',',',$search);
  	  $SITE_KEYS.=',arama,aranan,'.$search;
  	break;
  	case 'sendact':
  	  $SITE_TITLE='Aktivasyon Gönder | '.$SITE_TITLE;
      $SITE_KEYS.=',aktivsyon,aktive,hesap,üyelik';
  	break;
  	case 'users':
  	  $SITE_TITLE='Kullanıcı İşlemleri | '.$SITE_TITLE;
  	break;
  }
}

function getID($table) { // cetegory, page, item, brands
  global $mysql;
  $id=@$_GET['id'];
  if (empty($id)) $id=@$_GET['name'];

  if (empty($id)) return false;
  elseif (is_numeric($id)) {
    $id=(int)$id;
    $q=$mysql->query("select id from $table where id='$id'");
    if ($mysql->numRows($q)==1) return $id;
    else return false;
  }
  else {
    $name=$mysql->escape($id);
    $q=$mysql->query("select id from $table where name='$name'");
    if ($mysql->numRows($q)==1) {
      $id=$mysql->result($q,'id');
      return $id;
    }
    else return false;
  }
}

function getTitle($table) { // cetegory, page, item, brands
  global $mysql,$ITEMS_TABLE;
  $id=@$_GET['id'];
  if (empty($id)) $id=@$_GET['name'];

  $title='title';
  if ($table==$ITEMS_TABLE) $title='item';
  $q=false;

  if (empty($id)) return false;
  elseif (is_numeric($id)) {
    $id=(int)$id;
    $q=$mysql->query("select $title from $table where id='$id'");
  }
  else {
    $name=$mysql->escape($id);
    $q=$mysql->query("select $title from $table where name='$name'");
  }

  if ($mysql->numRows($q)==1) {
    $id=$mysql->result($q,$title);
    return $id;
  }
  else return false;
}

function findPage() {
  $page=@(int)$_GET['p'];
  if ($page<1) {
    $request=parse_url($_SERVER['REQUEST_URI']);
    preg_match('@^/.+/([0-9]+)$@i',$request['path'],$match);
    $page=$match[1];
    if (!empty($page)&&$page>0) return $page;
    else return '1';
  }
  else return $page;
}

function goBack() {
  global $SITE_URL,$_LANG;
  $ref=$_SERVER['HTTP_REFERER'];
  if (eregi($SITE_URL,$ref)) {
    $return='<br /><a href="'.$ref.'" title="'.$_LANG['general']['goback'].'">'.$_LANG['general']['goback'].'</a><br />';
    return $return;
  }
  return false;
}

function getCatId($name) {
  global $CATEGORIES_TABLE,$mysql;

  $name=$mysql->escape(urldecode($name));
  $q=$mysql->query("select id from $CATEGORIES_TABLE where name='$name'");
  if ($mysql->numRows($q)==1) {
    $id=$mysql->result($q,'id');
    return $id;
  }
  else return false;
}

function getUsers() {
  global $mysql,$USERS_TABLE,$prep;
  //id, email, password, name, lang, date, level, bdate, gender, phone, address, city

  $out=array();
  $i=0;
  $q=$mysql->query("select * from $USERS_TABLE order by name asc");
  while ($read=$mysql->fetch($q)) {
    $out[$i]=$read;
    $i++;
  }
  return $out;
}

function getUser($id) {
  global $mysql,$USERS_TABLE;

  $q=$mysql->query("select * from $USERS_TABLE where id='$id'");
  $fetch=$mysql->fetch($q);
  return $fetch;
}

function getBrands() {
  global $BRANDS_TABLE,$mysql;
  $active="and active='1'";
  if (isAdmin()) $active='';

  $q=$mysql->query("select * from $BRANDS_TABLE where 1=1 $active order by `order` asc");
  if ($mysql->numRows($q)>0) {
    $i=0;
    $return='';
    while ($read=$mysql->fetch($q)) {
      $return[$i]=$read;
      $i++;
    }
    return $return;
  }
  else return false;
}

function getItem($id) {
  global $ITEMS_TABLE,$SITE_DEF_LANG,$mysql;

  $q=$mysql->query("select * from $ITEMS_TABLE where id='$id' and lang='$SITE_DEF_LANG'");
  if ($mysql->numRows($q)>0) {
    // id, item, summary, info, name, home, new, lang, show_price, xml_code, image, date, brand_id, active, order
    $return=$mysql->fetch($q);
    return $return;
  }
  else return false;
}

function getItemAll($id) {
  global $EXCHANGES_TABLE,$ITEMS_TABLE,$ITEM_PRICES_TABLE,$IMAGES_TABLE,$SITE_DEF_LANG,$mysql;

  $q=$mysql->query("select * from $ITEMS_TABLE where id='$id' and lang='$SITE_DEF_LANG'");
  if ($mysql->numRows($q)>0) {
    # get item
    // id, item, summary, info, name, home, new, lang, show_price, xml_code, image, date, brand_id, active, order
    $return['item']=$mysql->fetch($q);
    # get item

    # get item prices
    // id, item_id, exchange_id, price, info, campaign, campaign_price, active, vat, t_price, date
    // id, name, exchange, date, active
    $active="and $EXCHANGES_TABLE.active='1'";
    if (isAdmin()) $active='';
    $sql="select $ITEM_PRICES_TABLE.*,$EXCHANGES_TABLE.name as ename,$EXCHANGES_TABLE.exchange from $ITEM_PRICES_TABLE,$EXCHANGES_TABLE ";
    $sql.="where $ITEM_PRICES_TABLE.item_id='$id' and $ITEM_PRICES_TABLE.exchange_id=$EXCHANGES_TABLE.id $active";
    $q=$mysql->query($sql);
    if ($mysql->numRows($q)>0) {
      $i=0;
      while ($read=$mysql->fetch($q)) {
        $return['prices'][$i]=$read;
        $i++;
      }
    }
    else $return['prices']=false;
    # / get item prices

    # get item images
    // id, item_id, image, active, date
    $active="and active='1'";
    if (isAdmin()) $active='';
    $q=$mysql->query("select * from $IMAGES_TABLE where item_id='$id' $active");
    if ($mysql->numRows($q)>0) {
      $i=0;
      while ($read=$mysql->fetch($q)) {
        $return['images'][$i]=$read;
        $i++;
      }
    }
    else $return['images']=false;
    # / get item images

    return $return;
  }
  else return false;
}

function homeItems() {
  global $ITEM_HOME_TOTAL,$SITE_DEF_LANG,$mysql,$EXCHANGES_TABLE,$ITEMS_TABLE,$ITEM_PRICES_TABLE;
  /*
  $ITEMS_TABLE: id, item, summary, info, name, home, new, lang, show_price, xml_code, image, date, brand_id, active, order
  $ITEM_CATS_TABLE: id, item_id, cat_id, primary
  $EXCHANGES_TABLE: id, name, exchange, date, active
  $ITEM_PRICES_TABLE: id, item_id, exchange_id, price, info, campaign, campaign_price, active, vat, t_price, date

  RESULT: id, item, summary, info, name, home, new, lang, show_price, xml_code, image, ename, exchange, price, info, campaign, campaign_price,
  */
  /*
  $active="and $EXCHANGES_TABLE.active='1' and $ITEMS_TABLE.active='1'";
  if (isAdmin()) $active='';
  $sql="select $ITEMS_TABLE.*,$EXCHANGES_TABLE.name as ename,$EXCHANGES_TABLE.exchange,$ITEM_PRICES_TABLE.price,$ITEM_PRICES_TABLE.info,";
  $sql.="$ITEM_PRICES_TABLE.campaign,$ITEM_PRICES_TABLE.campaign_price,$ITEM_PRICES_TABLE.item_id from $ITEMS_TABLE,$ITEM_PRICES_TABLE,$EXCHANGES_TABLE ";
  $sql.="where $ITEMS_TABLE.home='1' and $ITEMS_TABLE.lang='$SITE_DEF_LANG' $active and ";
  $sql.="$ITEM_PRICES_TABLE.item_id=$ITEMS_TABLE.id and $ITEM_PRICES_TABLE.exchange_id=$EXCHANGES_TABLE.id ";
  $sql.="group by $ITEM_PRICES_TABLE.item_id order by rand(now()) limit 0,$ITEM_HOME_TOTAL";
  */
  $active=" and it.active='1'";
  if (isAdmin()) $active='';
  $sql="select
    it.*,
    ip.price, ip.info as p_info, ip.campaign, ip.campaign_price, ip.active as p_active,
    ex.name as ex_name, ex.exchange, ex.active as e_active
    from $ITEMS_TABLE as it
    left join $ITEM_PRICES_TABLE as ip on it.id=ip.item_id
    left join $EXCHANGES_TABLE as ex on ex.id=ip.exchange_id
    where it.home='1' $active and  it.lang='$SITE_DEF_LANG' group by it.id order by rand(now()) limit 0,$ITEM_HOME_TOTAL";

  $q=$mysql->query($sql);
  if ($mysql->numRows($q)>0) {
    $i=0;
    $return='';
    while ($read=$mysql->fetch($q)) {
      $return[$i]=$read;
      $i++;
    }
    return $return;
  }
  return false;
}

function listItems($cat_id,$total,$start=0) {
  global $EXCHANGES_TABLE,$ITEM_CATS_TABLE,$ITEMS_TABLE,$ITEM_PRICES_TABLE,$SITE_DEF_LANG,$mysql;
  $out=false;
  /*
  $ITEMS_TABLE: id, item, summary, info, name, home, new, lang, show_price, xml_code, image, date, brand_id, active, order
  $ITEM_CATS_TABLE: id, item_id, cat_id, primary
  $EXCHANGES_TABLE: id, name, exchange, date, active
  $ITEM_PRICES_TABLE: id, item_id, exchange_id, price, info, campaign, campaign_price, active, vat, t_price, date

  RESULT: id, item, summary, info, name, home, new, lang, show_price, xml_code, image, ename, exchange, price, info, campaign, campaign_price,
  */

/*
  $active="and $EXCHANGES_TABLE.active='1' and $ITEMS_TABLE.active='1'";
  $price_sql="$ITEM_PRICES_TABLE.item_id=$ITEMS_TABLE.id and $ITEM_PRICES_TABLE.exchange_id=$EXCHANGES_TABLE.id and";
  if (isAdmin()) {
    $active='';
    $price_sql="";
  }

  $sql="select $ITEMS_TABLE.*,$EXCHANGES_TABLE.name as ename,$EXCHANGES_TABLE.exchange,$ITEM_PRICES_TABLE.price,$ITEM_PRICES_TABLE.active as price_active,";
  $sql.="$ITEM_PRICES_TABLE.info,$ITEM_PRICES_TABLE.campaign,$ITEM_PRICES_TABLE.campaign_price,$ITEM_PRICES_TABLE.item_id from $ITEMS_TABLE,$ITEM_CATS_TABLE,";
  $sql.="$ITEM_PRICES_TABLE,$EXCHANGES_TABLE where $ITEM_CATS_TABLE.cat_id='$cat_id' and $ITEM_CATS_TABLE.item_id=$ITEMS_TABLE.id and ";
  $sql.="$ITEM_PRICES_TABLE.item_id=$ITEMS_TABLE.id and $ITEM_PRICES_TABLE.exchange_id=$EXCHANGES_TABLE.id and $ITEMS_TABLE.lang='$SITE_DEF_LANG' ";
  $sql.="$active group by $ITEM_PRICES_TABLE.item_id order by $ITEMS_TABLE.`order` asc limit $start,$total";
*/

  // id, item, summary, info, name, home, new, lang, show_price, xml_code, image, date, brand_id, active, order, stock,
  // price, p_info, campaign, campaign_price, p_active, ex_name, exchange, e_active
  $active=" and it.active='1'";
  if (isAdmin()) $active='';
  $sql="select
      it.*,
      ip.price, ip.info as p_info, ip.campaign, ip.campaign_price, ip.active as p_active,
      ex.name as ex_name, ex.exchange, ex.active as e_active
      from $ITEM_CATS_TABLE as ic,$ITEMS_TABLE as it
      left join $ITEM_PRICES_TABLE as ip on it.id=ip.item_id
      left join $EXCHANGES_TABLE as ex on ex.id=ip.exchange_id
      where ic.cat_id='$cat_id' and ic.item_id=it.id $active and it.lang='$SITE_DEF_LANG'
      group by it.id order by it.`order` asc limit $start,$total";

  $q=$mysql->query($sql);
  if ($mysql->numRows($q)>0) {
    $out=array();
    while ($read=$mysql->fetch($q)) $out[]=$read;
    return $out;
  }
  else return $out;

 return $out;
}

function listSubCats($parent_cat) {
  global $CATEGORIES_TABLE,$SITE_DEF_LANG,$SITE_SEO,$SITE_URL,$mysql,$prep;
  $out='';
  $active="and active='1'";
  if (isAdmin()) $active='';
  // id, title, info, name, active, parent, order, lang, date
  $q=$mysql->query("select id,name,info,title from $CATEGORIES_TABLE where parent='$parent_cat' $active and lang='$SITE_DEF_LANG' order by `order` asc");
  if ($mysql->numRows($q)>0) {
    $out='';

    while ($read=$mysql->fetch($q)) {
      $id=$read['id'];
      $name=$read['name'];
      $desc=$prep->html($read['info']);
      $title=$prep->html($read['title']);

      if ($SITE_SEO) $out.='<a href="'.$SITE_URL.'/category/'.$name.'" title="'.$desc.'">'.$title.'</a> &brvbar; ';
      else $out.='<a href="index.php?page=category&id='.$id.'" title="'.$desc.'">'.$title.'</a> &brvbar; ';
    } // while
    return $out;
  } // if
  else return $out;
}

function listCatParents($parent,$out='') {
  global $CATEGORIES_TABLE,$SITE_DEF_LANG,$SITE_SEO,$SITE_URL,$mysql,$prep;

  // id, title, info, name, active, parent, order, lang, date
  $active="and active='1'";
  if (isAdmin()) $active='';
  $q=$mysql->query("select id,name,info,title,parent from $CATEGORIES_TABLE where id='$parent' $active and lang='$SITE_DEF_LANG'");
  if ($mysql->numRows($q)>0) {
    $read=$mysql->fetch($q);
    $id=$read['id'];
    $name=$read['name'];
    $parent=$read['parent'];
    $desc=$prep->html($read['info']);
    $title=$prep->html($read['title']);

    if ($SITE_SEO) $out='<a href="'.$SITE_URL.'/category/'.$name.'" title="'.$desc.'">'.$title.'</a> &raquo; '.$out;
    else $out='<a href="index.php?page=category&id='.$id.'" title="'.$desc.'">'.$title.'</a> &raquo; '.$out;
    if ($parent!=0) $out=listCatParents($parent,$out);
    else return $out;
  }
  return $out;
}

function listTopMenu() {
  global $PAGES_TABLE,$SITE_URL,$SITE_SEO,$SITE_DEF_LANG,$mysql,$prep;
  $topMenu='';
  $active="and active='1'";
  if (isAdmin()) $active='';
  // id, title, content, info, name, date, order, active, lang
  $q=$mysql->query("select * from $PAGES_TABLE where lang='$SITE_DEF_LANG' $active order by `order` asc");
  while ($read=$mysql->fetch($q)) {
  	$id=$read['id'];
  	$title=$prep->html($read['title']);
  	$desc=$prep->html($read['info']);
  	$name=$read['name'];

  	if (!$SITE_SEO) $topMenu.='<li><a href="index.php?page=page&id='.$id.'" title="'.$desc.'">'.$title.'</a></li>';
  	else $topMenu.='<li><a href="'.$SITE_URL.'/page/'.$name.'" title="'.$desc.'">'.$title.'</a></li>';
  }
  return $topMenu;
}

function listLeftMenu($parent) {
  global $CATEGORIES_TABLE,$SITE_URL,$SITE_SEO,$mysql,$prep,$SITE_DEF_LANG;
  $active="and active='1'";
  if (isAdmin()) $active='';
  // id, title, info, name, active, parent, order, lang, date
  $q=$mysql->query("select id,title,info,name from $CATEGORIES_TABLE where parent='$parent' $active and lang='$SITE_DEF_LANG' order by `order` asc");
  if ($mysql->numRows($q)>0) {
    $out='<ul>';
    $class='';
    if ($parent==0) {
      $out='<ul id="lmenu">';
      //$class=' class="menuhead"';
    }

    while ($read=$mysql->fetch($q)) {
    	$id=$read['id'];
	    $title=$prep->html($read['title']);
	    $desc=$prep->html($read['info']);
    	$name=$read['name'];
    	if (empty($desc)) $desc=$title;

    	$out.='<li>';
    	if (!$SITE_SEO) $out.='<a href="index.php?page=category&id='.$id.'" title="'.$desc.'"'.$class.'>'.$title.'</a>';
    	else $out.='<a href="'.$SITE_URL.'/category/'.$name.'" title="'.$desc.'"'.$class.'>'.$title.'</a>';
    	if ($parent==0) $out.=listLeftMenu($id);
    	$out.='</li>';
    }
    $out.='</ul>';
    return $out;
  }
}

function listLangs($selected='') {
  global $SITE_DEF_LANG,$SITE_LANGUAGES;

  if (empty($selected)) $selected=$SITE_DEF_LANG;
  $out='<select name="lang" id="lang">';
  foreach ($SITE_LANGUAGES as $key=>$val) {
    $out.='<option value="'.$key.'"';
    if ($val==$SITE_DEF_LANG) $out.=' selected';
    $out.='>'.$val.'</option>';
  }
  return $out.'</select>';
}

function listCities($selected='') {
  global $CITIES;

  $out='<select name="city" id="city">';
  foreach ($CITIES as $key=>$val) {
    $out.='<option value="'.$key.'"';
    if ($key==$selected) $out.=' selected';
    $out.='>'.$val.'</option>';
  }
  return $out.'</select>';
}

function listBrands($selected='') {
  global $BRANDS_TABLE,$mysql,$prep;
  $active="and active='1'";
  if (isAdmin()) $active='';
  $out='<select name="brand" id="brand">';
  $q=$mysql->query("select * from $BRANDS_TABLE where 1=1 $active order by `order` asc");
  while ($read=$mysql->fetch($q)) {
    $id=$read['id'];
    $title=$prep->html($read['name']);

    $out.='<option value="'.$id.'"';
    if ($id==$selected) $out.=' selected';
    $out.='>'.$title.'</option>';
  }
  return $out.'</select>';
}

function listexchanges($selected='') {
  global $EXCHANGES_TABLE,$mysql,$prep;
  //id, name, exchange, date, active
  $active="active='1'";
  if (isAdmin()) $active='';
  $out='<select name="exhange_id" id="exhange_id">';
  $q=$mysql->query("select * from $EXCHANGES_TABLE where 1=1  $active order by name asc");
  while ($read=$mysql->fetch($q)) {
    $id=$read['id'];
    $title=$prep->html($read['name']);

    $out.='<option value="'.$id.'"';
    if ($id==$selected) $out.=' selected';
    $out.='>'.$title.'</option>';
  }
  return $out.'</select>';
}

function loadModules() {
  global $MODULES;
  foreach ($MODULES as $module) {
    require_once("modules/$module.php");
  }
}

function setLanguage() {
  global $SITE_DEF_LANG;
  if (@!empty($_SESSION['lang'])) $SITE_DEF_LANG=$_SESSION['lang'];
  if (@!empty($_GET['lang'])) $SITE_DEF_LANG=$_GET['lang'];

  switch ($SITE_DEF_LANG) {
  	case 'en': require_once('lang/en.php'); break;
  	case 'tr': require_once('lang/tr.php'); break;
  	default: require_once('lang/tr.php'); $SITE_DEF_LANG='tr'; break;
  }
  $_SESSION['lang']=$SITE_DEF_LANG;
}

function createPass($length=6) {
  $pass='';
  for ($i=0;$i<$length;$i++) $pass.=mt_rand(0,9);
  return $pass;
}

function sendMail($toMail,$fromMail,$subject,$message) {
	$headers="MIME-Version: 1.0\n";
	$headers.="Content-type: text/html; charset=utf-8\n";
	$headers.="X-Mailer: PHP\n";
	$headers.="X-Sender: <PHP>\n";
	$headers.="From: <$fromMail>\n";
	$headers.="Return-Path: <$fromMail>\n";
	$mail=@mail($toMail,$subject,$message,$headers);
	if ($mail) return true;
	else return false;
}

function makeNicer($text) {
  global $prep;
  $text=$prep->replace(' ','-',$text);
  $text=preg_replace('@[^A-Za-z0-9öçşığüÖÇŞİĞÜ\-]+@i','',$text);
  $text=ereg_replace('[-]+','-',$text);
  $text=$prep->lower($text);
  $text=trim($text,'-');
  return $text;
}

function checkNiceName($name,$table,$option='insert',$id=0) {
  global $mysql;
  if ($option=='insert') {
    $q=$mysql->query("select id from $table where name='$name'");
    if ($mysql->numRows($q)>0) {
      $q=$mysql->query("select id from $table order by id desc");
      $lastID=$mysql->result($q,'id');
      $lastID++;
      $name=$name.$lastID;
      return checkNiceName($name,$table);
    }
    else return $name;
  }
  elseif ($option=='update') {
    $q=$mysql->query("select id from $table where name='$name' and id!='$id'");
    if ($mysql->numRows($q)>0) {
      $q=$mysql->query("select id from $table order by id desc");
      $lastID=$mysql->result($q,'id');
      $lastID++;
      $name=$name.$lastID;
      return checkNiceName($name,$table,'update',$id);
    }
    else return $name;
  }
}

function wordCut($str,$length,$cutType=1) {
  $total=0;
  $words=str_word_count(trim($str),1);

  foreach ($words as $key=>$word) {
    $total+=strlen(trim($word));
    if ($total>=$length) {
      return implode(' ',array_slice($words,0,$key+$cutType));
      break;
    }
  }
  if ($total<$length) return $str;
  else return false;
}
?>